<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiAdmin/layui/css/layui.css" media="all">
    <style>
        .layui-form-select dl {
            max-height: 120px;
        }
        /*  设置lable的宽度
        .layui-form-label {
            width: 100px;
        }
        .layui-input-block {
            margin-left: 130px;
        }*/
    </style>
</head>

<body>
    <div class="layui-form" id="popup-from" style="padding: 20px 30px 0 30px;">
        <input type="text" name="cId" value="" style="display: none;" />
        <div class="layui-form-item layui-row layui-col-space20">
            <div class="layui-col-xs6">
                <label class="layui-form-label">角色名</label>
                <div class="layui-input-block">
                    <input type="text" name="cName" lay-verify="required" placeholder="请输入角色名" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">是否启用</label>
                <div class="layui-input-block">
                    <input type="radio" name="cEnabled" value="true" title="是" checked>
                    <input type="radio" name="cEnabled" value="false" title="否">
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input type="text" name="cSortCode" autocomplete="off" lay-verify="required|number"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">描述</label>
                <div class="layui-input-block">
                    <input type="text" name="cDescription" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-col-xs12">
                <label class="layui-form-label">权限</label>
                <div class="layui-input-block">
                    <!-- <div class="layui-inline" id="auths" style="width:180px ;"></div> -->
                    <div id="auths"></div>
                </div>
            </div>
            <div class="layui-form-item layui-layout-admin layui-hide">
                <div class="layui-input-block">
                    <div class="layui-footer" style="left: 0; text-align: right">
                        <button class="layui-btn" lay-filter="form-submit" id="form-submit" type="button" lay-submit>
                            立即提交
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="../../layuiAdmin/layui/layui.js"></script>
    <script src="../../layuiAdmin/lib/extend/xm-select.js"></script>
    <script>
        layui.config({
            base: '../../layuiAdmin/', //静态资源所在路径
            version: top.layui.setter.version
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'form', 'tree'], function () {
            var $ = layui.$,
                form = layui.form,
                admin = layui.admin,
                tree = layui.tree
            var id = $.getParam("id");
            var demo1 = xmSelect.render({
                el: '#auths',
                name: 'auths',
                tips: '请选择权限',
                model: {
                    label: {
                        type: 'xxxx', //自定义与下面的对应
                        xxxx: {
                            template(data, sels) {
                                return "已选中 " + sels.length + " 项"
                            }
                        },
                    }
                },
                autoRow: true,
                tree: {
                    show: true,
                    showFolderIcon: true,
                    showLine: true,
                    indent: 20,
                    strict: false,
                    clickCheck: true,
                    expandedKeys: [],
                },
                height: 'auto',
                data: [],
            //动态赋值   
            //    data() {
            //    var data = null;
            //    admin.req({
            //        type: 'get',
            //        async: false,
            //        url: '/api/Clinic/GetClinicsSelect',
            //        success: function (r) {
            //            if (r.code == 200) {
            //                data = r.data;
            //            }
            //        }
            //    });
            //    return data;
            //}
                //单选需配置的
                //layVerify: 'required',
                //radio: true,
                //clickClose: true,
                //toolbar: {
                //    show: true,
                //},
                //tree: {
                //    //是否显示树状结构
                //    show: true,
                //    strict: false
                //},
                //iconfont: {
                //    parent: 'hidden',
                //},
                //filterable: true,
                //height: '300px',

            });
            admin.req({
                type: 'get',
                async: false,
                url: '/api/Role/GetRoleAuthList?roleId=' + id,
                success: function (r) {
                    if (r.code == 200) {
                        demo1.update({
                            data: r.data.info
                        });
                        demo1.changeExpandedKeys(r.data.expandedKeys)
                    }
                }
            });
            //获取表单值
            if (id != "") {
                admin.req({
                    type: 'get',
                    url: "/api/Role/GetRoleById?id=" + id,
                    success: function (r) {
                        if (r.code == 200) {
                            $("#popup-from").SetFormData(r.data);
                            form.render();
                        }
                    }
                });
            }
            //保存表单
            form.on('submit(form-submit)', function (data) {
                var field = data.field; //获取提交的字段
                var selectArr = demo1.getValue();
                field.authLists = selectArr.map(t => { return { ItemId: t.value, ItemType: t.itemType } });
                //提交
                admin.req({
                    type: 'post',
                    url: "/api/Role/SaveRoleAuth",
                    data: field,
                    success: function (data) {
                        if (data.code == 200) {
                            parent.layer.msg('操作成功!', { icon: 1, time: 1000 }, function () {
                                parent.layui.table.reload(parent.areaName);
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg('操作失败!\n' + data.msg, { icon: 2, time: 2000 });
                        }
                    }
                });
            });
        })
    </script>
</body>

</html>